package com.support.SQLite;

import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.base.Constant;
import com.support.SQLite.NewsDao.NewsEnum;

public class SQLHelper extends SQLiteOpenHelper {
	private static final String DB_NAME = Constant.DB_NAME;
	private static final int DB_VERSION = Constant.DB_VERSION;
	public static final String COLUMN_ID = "_id";

	public SQLHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(creatTable(NewsEnum.TAB_NAME + "", NewsEnum.getAllcolumns()));
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int olderVersion, int newVersion) {
		db.execSQL(dropTable(NewsEnum.TAB_NAME + ""));

		onCreate(db);

	}

	public String creatTable(String TAB_NAME, List<String> columns) {
		StringBuilder strBuilder = new StringBuilder("create table " + TAB_NAME + "(" + COLUMN_ID
				+ " integer primary key autoincrement, ");
		for (String columnName : columns) {
			strBuilder.append(columnName + " NVARCHAR, ");
		}
		strBuilder.append(");");
		return strBuilder.toString();
	}

	public String dropTable(String tabName) {
		return "DROP TABLE IF EXISTS " + tabName;
	}

}
